<template>
  <a-modal v-model:open="visible" title="商户列表" width="80%" :footer="null" @cancel="handleCancel">
    <a-form class="smart-query-form">
      <a-row class="smart-query-form-row">
        <a-form-item label="商户名称" class="smart-query-form-item">
          <a-input style="width: 200px" v-model:value="queryForm.companyNameLeftLike" placeholder="商户名称" />
        </a-form-item>
        <a-form-item label="状态" class="smart-query-form-item">
          <a-select style="width: 200px" v-model:value="queryForm.status" placeholder="状态" :options="ZZB_MCH_NO_STATUS" />
        </a-form-item>
        <a-form-item label="进件状态" class="smart-query-form-item">
          <a-select style="width: 200px" v-model:value="queryForm.registerStatus" placeholder="进件状态" :options="REGISTER_STATUS" />
        </a-form-item>
        <a-form-item class="smart-query-form-item">
          <a-button type="primary" @click="fetchMerchantData">
            <template #icon>
              <SearchOutlined />
            </template>
            查询
          </a-button>
          <a-button @click="resetQuery" class="smart-margin-left10">
            <template #icon>
              <ReloadOutlined />
            </template>
            重置
          </a-button>
        </a-form-item>
      </a-row>
    </a-form>
    <a-table size="small" :dataSource="merchantData" :columns="merchantColumns" rowKey="id" bordered :loading="loading" :pagination="false">
      <template #bodyCell="{ text, column }">
        <!-- 自定义渲染示例 -->

        <template v-if="column.dataIndex === 'status'">
          <a-tag :color="statusTypeColorArray[text]">{{ $smartEnumPlugin.getDescByValue('ZZB_NO_STATUS_ENUM', text) }}</a-tag>
        </template>
      </template>
    </a-table>
    <div class="smart-query-table-page">
      <a-pagination
        showSizeChanger
        showQuickJumper
        show-less-items
        :page_sizeOptions="PAGE_SIZE_OPTIONS"
        :defaultpage_size="queryForm.pageSize"
        v-model:current="queryForm.pageNo"
        v-model:page_size="queryForm.pageSize"
        :total="total"
        @change="queryData"
        @showSizeChange="queryData"
        :show-total="(total) => `共${total}条`"
      />
    </div>
  </a-modal>
</template>

<script setup>
  import { reactive, ref } from 'vue'
  import { message } from 'ant-design-vue'
  import { exhibitionMerchantPage } from '/@/api/business/b-agent/merchant/merchant-api'
  import { PAGE_SIZE_OPTIONS } from '/@/constants/common-const.js'
  import { ZZB_NO_STATUS_ENUM } from '/@/constants/system/system-const.js'
  import { selectDictLabel } from '/@/utils/common'
  import dayjs from 'dayjs'
  import {
    ZZB_MCH_NO_STATUS, // 代理商状态
    REGISTER_STATUS, // 进件状态
  } from '/@/constants/dict'

  const visible = ref(false)
  const loading = ref(false)

  const statusTypeColorArray = ['red', 'blue']

  const queryFormState = {
    companyNameLeftLike: undefined, //代理商名称
    status: undefined,
    registerStatus: undefined,
    exhibitionId: undefined, //业务员ID
    pageNo: 1,
    pageSize: 10,
  }
  // 总数
  const total = ref(0)
  const queryForm = ref({ ...queryFormState })
  const merchantData = ref([])
  const merchantColumns = ref([
    {
      title: '序号',
      dataIndex: 'index',
      customRender: ({ index }) => {
        return (queryForm.value.pageNo - 1) * queryForm.value.pageSize + index + 1
      },
    },
    {
      title: '商户账号',
      dataIndex: 'companyName',
      ellipsis: true,
    },
    {
      title: '联系人',
      dataIndex: 'name',
      ellipsis: true,
    },
    {
      title: '联系方式',
      dataIndex: 'phone',
      ellipsis: true,
    },
    {
      title: '地址',
      dataIndex: 'addr',
      ellipsis: true,
    },
    {
      title: '进件状态',
      dataIndex: 'registerStatus',
      customRender: ({ text }) => {
        return selectDictLabel(REGISTER_STATUS, text)
      },
      ellipsis: true,
    },
    {
      title: '进件审核通过时间',
      dataIndex: 'registerPassTime',
      customRender: (column) => dayjs(column.text).format('YYYY-MM-DD HH:mm:ss'),
      ellipsis: true,
    },
    {
      title: '是否为活跃商户',
      dataIndex: 'isActive',
      customRender: (column) => (column.text ? '是' : '否'),
      ellipsis: true,
    },
    {
      title: '是否为交易商户',
      dataIndex: 'isTradeAccount',
      customRender: (column) => (column.text ? '是' : '否'),
      ellipsis: true,
    },
    {
      title: '是否为流失商户',
      dataIndex: 'isFlowAccount',
      customRender: (column) => (column.text ? '是' : '否'),
      ellipsis: true,
    },
    {
      title: '状态',
      dataIndex: 'status',
      customRender: ({ text }) => {
        return selectDictLabel(ZZB_MCH_NO_STATUS, text)
      },
      ellipsis: true,
    },
  ])

  const emit = defineEmits(['cancel'])

  // 打开弹窗

  function show(record) {
    queryForm.value.exhibitionId = record?.exhibitionId ? record.exhibitionId : undefined
    fetchMerchantData(record)
    visible.value = true
    loading.value = true
  }

  // 关闭弹窗
  function handleCancel() {
    visible.value = false
    emit('cancel')
  }

  async function fetchMerchantData(record) {
    try {
      const params = {
        ...queryForm.value,
      }
      const data = await exhibitionMerchantPage(params)
      merchantData.value = data.data
      total.value = data.total
    } catch (error) {
      console.log(error)

      message.error('加载商户信息失败')
    } finally {
      loading.value = false
    }
  }

  defineExpose({
    show,
  })
</script>
